package com.studyyoun.android.nonghe.welcom;
/**
 * Created by zhaolong on 2017/10/3.
 * 站在顶峰，看世界
 * 落在谷底，思人生
 */

import android.content.Intent;
import android.text.TextUtils;

import com.studyyoun.android.androidcommonbaselibrary.utils.FastJsonUtil;
import com.studyyoun.android.androidcommonbaselibrary.utils.LogUtils;
import com.studyyoun.android.androidcommonbaselibrary.utils.SharedPreferencesUtil;
import com.studyyoun.android.nonghe.welcom.activity.LoginActivity;
import com.studyyoun.android.nonghe.welcom.model.UserModel;

/**
 * class infation
 */
public class CommonUserHelper {
    private final static String USER_DATA_CACHE_KEY = "wisdom_user_data";
    private final static String USER="user_che";
    //用户ID
    private static UserModel sUserModel;
    //TODO 1 清除用户信息
    public void clearUserData() {

        sUserModel=null;
        SharedPreferencesUtil.getInstance(App.getInstance()).removeData(USER_DATA_CACHE_KEY);
    }

    public static  UserModel getUserModel(){
        if (sUserModel==null) {
            getUserData();
        }
        return sUserModel;
    }

    private static void getUserData() {

        String userData = SharedPreferencesUtil.getInstance(App.getInstance()).getData(USER);
        if (!TextUtils.isEmpty(userData)) {
            try {
                sUserModel = FastJsonUtil.toBean(userData, UserModel.class);
            } catch (Exception e) {
                e.printStackTrace();
                LogUtils.e("格式化数据异常 "+e.getMessage());
            }
        }
    }

    public static void saveUserData(UserModel content) {
        if (content != null) {
            try {
                sUserModel=content;
                SharedPreferencesUtil.getInstance(App.getInstance()).saveData(USER,FastJsonUtil.toJSONString(content));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void exitUser() {
        sUserModel=null;
        SharedPreferencesUtil.getInstance(App.getInstance()).removeData(USER);
    }

    public static void toLoginPage() {
        Intent intent = new Intent(App.getInstance(), LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        App.getInstance().startActivity(intent);
    }
}
